package dev.ece.util.obj;

import java.util.Observable;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

/**
 * 计数
 * @author 潜行的虚影
 * @param <T>
 */
public class ObjectCount<T> extends Observable {

	private String name;
	
	private int count;
	
	private T object;
	
	public ObjectCount(String name, T object) {
		this.name = name;
		this.object = object;
	}
	
	public void addCount() {
		this.count ++;
		this.setChanged();
		this.notifyObservers(this);
	}
	
	public void deleteCount() {
		this.count --;
		this.setChanged();
		this.notifyObservers(this);
	}

	public boolean needDestroy() {
		return this.count == 0;
	}
	
	public String getName() {
		return name;
	}

	public int getCount() {
		return count;
	}

	public T getObject() {
		return object;
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder()
				.append(this.name).toHashCode();
	}

	@Override
	@SuppressWarnings("unchecked")
	public boolean equals(Object obj) {
		if(obj == null) {
			return false;
		}
		if(!(obj instanceof ObjectCount)) {
			return false;
		}
		if(obj == this) {
			return true;
		}
		ObjectCount<T> count = (ObjectCount<T>) obj;
		return new EqualsBuilder()
				.append(count.name, this.name).isEquals();
	}
	
}
